﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;

namespace OneMG.Phoenix.Service
{
    public class ProductPhotosService : IProductPhotosService
    {
        IProductPhotos productPhotos = new ProductPhotosRepository();

        public System.Int32 AddProductPhotos(ProductPhotosModel model)
        {
            EProductPhotos entity = ConvertModelToEntity(model);

            return productPhotos.AddProductPhotos(entity);
        }

        public void UpdateByPhotoID(ProductPhotosModel model)
        {
            EProductPhotos entity = ConvertModelToEntity(model);

            productPhotos.UpdateByPhotoID(entity);

        }

        public void DeleteByPhotoID(System.Int32 photoID)
        {

            productPhotos.DeleteByPhotoID(photoID);

        }

        public void DeleteByProductID(System.Int32 productID)
        {
            productPhotos.DeleteByProductID(productID);
        }


        public ProductPhotosModel GetByPhotoID(System.Int32 photoID)
        {

            EProductPhotos entity = productPhotos.GetByPhotoID(photoID);

            ProductPhotosModel model = ConvertEntityToModel(entity);


            return model;
        }


        public IList<ProductPhotosModel> GetListByProductID(System.Int32 productID)
        {
            IList<EProductPhotos> entityList = productPhotos.GetListByProductID(productID);

            IList<ProductPhotosModel> modelList = new List<ProductPhotosModel>();

            foreach (var item in entityList)
            {
                ProductPhotosModel model = ConvertEntityToModel(item);
                if (model != null)
                {
                    modelList.Add(model);
                }
            }
            return modelList;

        }


        public ProductPhotosModel ConvertEntityToModel(EProductPhotos entity)
        {
            if (entity == null)
            {
                return null;
            }
            ProductPhotosModel model = new ProductPhotosModel();
            model.PhotoID = entity.PhotoID;
            model.ProductID = entity.ProductID;
            model.Photo = entity.Photo;
            model.CreateTime = entity.CreateTime;
            return model;
        }

        public EProductPhotos ConvertModelToEntity(ProductPhotosModel model)
        {
            if (model == null)
            {
                return null;
            }
            EProductPhotos entity = new EProductPhotos();
            entity.PhotoID = model.PhotoID;
            entity.ProductID = model.ProductID;
            entity.Photo = model.Photo;
            entity.CreateTime = model.CreateTime;
            return entity;
        }
    }
}